package com.zst.microservicenodeone.interceptor;

import com.zst.microservicenodeone.context.UserContext;
import com.zst.microservicenodeone.context.UserContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeignOauth2Interceptor implements RequestInterceptor {
    private final String HEADER = "Authorization";
    private final String BEARER = "Bearer";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        if(StringUtils.isNotEmpty(UserContextHolder.getContext().getAuthorization())){
            requestTemplate.header(UserContext.AUTHORIZATION, UserContextHolder.getContext().getAuthorization());
        }

    }
}
